package dimension.starry.gui.Bar;

import dimension.starry.StarryDimension;
import dimension.starry.dataManager.PlayerDataManager;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.registry.tag.FluidTags;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;

import static dimension.starry.constants.ExpLevel.needExp;
import static dimension.starry.constants.PlayerDataType.*;

/**
 * @author 150149
 */
@Environment(EnvType.CLIENT)
public class Hud {

    private static final Identifier HEALTH_BAR = new Identifier(StarryDimension.MOD_ID, "textures/gui/health_bar.png");
    private static final Identifier HUNGRY_BAR = new Identifier(StarryDimension.MOD_ID, "textures/gui/hungry_bar.png");
    private static final Identifier LEVEL_BALL_BACK = new Identifier(
        StarryDimension.MOD_ID, "textures/gui/level_ball_back.png");
    private static final Identifier LEVEL_BALL_FRONT = new Identifier(
        StarryDimension.MOD_ID, "textures/gui/level_ball_front.png");
    private static final Identifier LEVEL_BAR = new Identifier(StarryDimension.MOD_ID, "textures/gui/level_bar.png");
    private static final Identifier ICONS = new Identifier("textures/gui/icons.png");

    public static void onHudRender(DrawContext drawContext, InGameHud inGameHud) {
        ClientPlayerEntity player = MinecraftClient.getInstance().player;
        if (player == null) {
            return;
        }

        renderHealthBar(drawContext, player);
        renderHungryBar(drawContext, player);
        renderLeveBall(drawContext, player);
        renderAirBar(drawContext, player);
    }

    private static void renderAirBar(DrawContext drawContext, ClientPlayerEntity playerEntity) {
        int y = playerEntity.getMaxAir();
        int z = Math.min(playerEntity.getAir(), y);
        int n = MinecraftClient.getInstance().getWindow().getScaledWidth() / 2 + 91;
        if (playerEntity.isSubmergedIn(FluidTags.WATER) || z < y) {
            int aa = 2;
            int o = MinecraftClient.getInstance().getWindow().getScaledHeight() - 39;
            int t = o - 10;
            t -= aa * 10;
            int ab = MathHelper.ceil((double) (z - 2) * 10.0D / (double) y);
            int ac = MathHelper.ceil((double) z * 10.0D / (double) y) - ab;

            for (int ad = 0; ad < ab + ac; ++ad) {
                if (ad < ab) {
                    drawContext.drawTexture(ICONS, n - ad * 8 - 9, t, 16, 18, 9, 9);
                } else {
                    drawContext.drawTexture(ICONS, n - ad * 8 - 9, t, 25, 18, 9, 9);
                }
            }
        }
    }

    private static void renderHealthBar(DrawContext drawContext, ClientPlayerEntity player) {
        int width = MinecraftClient.getInstance().getWindow().getScaledWidth();
        int height = MinecraftClient.getInstance().getWindow().getScaledHeight();
        double healthPercent = player.getHealth() / player.getMaxHealth();
        long magic = PlayerDataManager.getLong(null, MAGIC);
        long maxMagic = PlayerDataManager.getLong(null, MAX_MAGIC);
        double magicPercent = magic / (double) maxMagic;
        TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;

        drawContext.drawTexture(HEALTH_BAR, width / 2 - 91, height / 2 + 80, 0, 9, 90, 9, 90, 28);
        drawContext.drawTexture(HEALTH_BAR, width / 2 - 91, height / 2 + 71, 0, 9, 90, 9, 90, 28);
        drawContext.drawTexture(HEALTH_BAR, width / 2 + 1, height / 2 + 80, 0, 9, 90, 9, 90, 28);
        drawContext.drawTexture(HEALTH_BAR, width / 2 + 1, height / 2 + 71, 0, 9, 90, 9, 90, 28);

        int h = (int) (88 * healthPercent);
        drawContext.drawTexture(HEALTH_BAR, width / 2 - 90, height / 2 + 81, 1, 1, h, 7, 90, 28);
        int m = (int) (88 * magicPercent);
        drawContext.drawTexture(HEALTH_BAR, width / 2 - 90, height / 2 + 72, 1, 19, m, 7, 90, 28);

        String healthNumber = (int) player.getHealth() + "/" + (int) player.getMaxHealth();
        String magicNumber = (int) magic + "/" + (int) maxMagic;
        drawContext.drawCenteredTextWithShadow(textRenderer, healthNumber, width / 2 - 45, height / 2 + 80, 16777215);
        drawContext.drawCenteredTextWithShadow(textRenderer, magicNumber, width / 2 - 45, height / 2 + 71, 16777215);
    }

    private static void renderHungryBar(DrawContext drawContext, ClientPlayerEntity player) {
        int width = MinecraftClient.getInstance().getWindow().getScaledWidth();
        int height = MinecraftClient.getInstance().getWindow().getScaledHeight();
        double foodLevel = player.getHungerManager().getFoodLevel();
        double hungryPercent = foodLevel / 20.0;
        int water = PlayerDataManager.getInt(null, WATER);
        double waterPercent = (double) water / (int) WATER.getDefaultValue.get();
        TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;

        int h = (int) (88 * hungryPercent);
        drawContext.drawTexture(HUNGRY_BAR, width / 2 + 2 + (88 - h), height / 2 + 81, 1 + (88 - h), 19, h, 7, 90, 28);
        int w = (int) (88 * waterPercent);
        drawContext.drawTexture(HUNGRY_BAR, width / 2 + 2 + (88 - w), height / 2 + 72, 1 + (88 - w), 1, w, 7, 90, 28);

        String hungryNumber = (int) foodLevel + "/20";
        String waterNumber = water / 10 + "/" + ((int) WATER.getDefaultValue.get() / 10);

        drawContext.drawCenteredTextWithShadow(textRenderer, hungryNumber, width / 2 + 45, height / 2 + 80, 16777215);
        drawContext.drawCenteredTextWithShadow(textRenderer, waterNumber, width / 2 + 45, height / 2 + 71, 16777215);
    }

    private static void renderLeveBall(DrawContext drawContext, ClientPlayerEntity player) {
        int width = MinecraftClient.getInstance().getWindow().getScaledWidth();
        int height = MinecraftClient.getInstance().getWindow().getScaledHeight();
        int level = PlayerDataManager.getInt(null, LEVEL);
        long exp = PlayerDataManager.getLong(null, EXP);
        long maxExp = needExp[level];

        drawContext.drawTexture(LEVEL_BAR, width / 2 - 20, height / 2 + 88, 0, 0, 39, 9, 39, 9);
        drawContext.drawTexture(LEVEL_BALL_FRONT, width / 2 - 10, height / 2 + 70, 0, 0, 20, 20, 20, 20);
        int x = (int) (exp / maxExp * 20);
        drawContext.drawTexture(LEVEL_BALL_BACK, width / 2 - 11, height / 2 + 69, 0, 0, 22, 22 - x, 22, 22);

        String expString = exp + "/" + maxExp;
        String mcLevel = String.valueOf(player.experienceLevel);
        TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
        drawContext.drawCenteredTextWithShadow(textRenderer, expString, width / 2, height / 2 + 88, 16777215);
        drawContext.drawCenteredTextWithShadow(
            textRenderer, String.valueOf(level), width / 2, height / 2 + 76, 16777215);
        drawContext.drawCenteredTextWithShadow(textRenderer, mcLevel, width / 2 - 96, height / 2 + 89, 2293580);
    }

}
